// 显示添加名言模态框
function showAddQuoteModal() {
    document.getElementById('addQuoteModal').style.display = 'block';
}

// 显示编辑名言模态框
function showEditQuoteModal(quoteId) {
    fetch(`/admin/quotes/${quoteId}`)
        .then(response => response.json())
        .then(data => {
            const form = document.getElementById('editQuoteForm');
            form.id.value = data.id;
            form.content.value = data.content;
            form.author.value = data.author;
            form.status.value = data.status;
            
            document.getElementById('editQuoteModal').style.display = 'block';
        });
}

// 关闭模态框
function closeModal(modalId) {
    document.getElementById(modalId).style.display = 'none';
}

// 添加名言
document.getElementById('addQuoteForm').addEventListener('submit', function(e) {
    e.preventDefault();
    
    const formData = new FormData(this);
    
    fetch('/admin/quotes/add', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: new URLSearchParams(formData)
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('添加成功');
            location.reload();
        } else {
            alert(data.message || '添加失败');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('添加失败，请重试');
    });
});

// 编辑名言
document.getElementById('editQuoteForm').addEventListener('submit', function(e) {
    e.preventDefault();
    
    const formData = new FormData(this);
    const quoteId = formData.get('id');
    
    fetch(`/admin/quotes/${quoteId}/update`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: new URLSearchParams(formData)
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('更新成功');
            location.reload();
        } else {
            alert(data.message || '更新失败');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('更新失败，请重试');
    });
});

// 删除名言
function deleteQuote(quoteId) {
    if (!confirm('确定要删除该名言吗？此操作不可恢复！')) {
        return;
    }
    
    fetch(`/admin/quotes/${quoteId}/delete`, {
        method: 'POST'
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('删除成功');
            location.reload();
        } else {
            alert(data.message || '删除失败');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('删除失败，请重试');
    });
} 